// Copyright (c) 2024-present AI-Labs

// @ ts-nocheck

/**
 * Base64字符串转File
 * @param base64String 
 * @param filename 
 * @returns 
 */
export function base64ToFile(base64String: string, filename: string) {
    var arr = base64String.split(','),
        mime = arr[0].match(/:(.*?);/)![1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, { type: mime });
}

/**
 * Base64字符串转Blob
 * @param base64String 
 * @returns 
 */
export function base64ToBlob(base64String: string) {
    var arr = base64String.split(','),
        mime = arr[0].match(/:(.*?);/)![1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}

/**
 * Base64字符串转URL
 * @param base64String 
 * @returns 
 */
export function base64ToUrl(base64String: string) {
    return URL.createObjectURL(base64ToBlob(base64String));
}
